calendar('./res/calendar.json', '2021-4');

// 功能：处理日历图数据
// 参数：链接/文件路径
// 返回：null
function calendar(path, yearandMonth){
  //$ajax()获取文件
  $.ajax({
    url: path,
    dataType: 'json'
  }).then(function (resp){
    //处理数据
    jsondata = resp.data;
    // console.log(jsondata);
    let calendarData = [];
    for (let i = 0; i < jsondata.length; i++) {
      calendarData.push([
      jsondata[i].date,
      jsondata[i].weatherCode,
      jsondata[i].weatherName,
      jsondata[i].trafficFlow,
      jsondata[i].trafficFlowCode,
      jsondata[i].AQILevel
      // jsondata[i].temperature,
      // jsondata[i].windSpeed,
      // jsondata[i].water,
      // jsondata[i].windSpeed,
      // jsondata[i].AQI,
    ])
  } 
    drawCalendar(yearandMonth, calendarData);
  })

  //D3.csv()函数读取本地文件
  // d3.json(path, function (jsondata) {
  //   let calendarData = [];
  //   for (let i = 0; i < jsondata.length; i++) {
  //       calendarData.push([
  //       jsondata[i].date,
  //       jsondata[i].weatherCode,
  //       jsondata[i].weatherName,
  //       jsondata[i].trafficFlow,
  //       jsondata[i].trafficFlowCode,
  //       jsondata[i].AQILevel
  //       // jsondata[i].temperature,
  //       // jsondata[i].windSpeed,
  //       // jsondata[i].water,
  //       // jsondata[i].windSpeed,
  //       // jsondata[i].AQI,
  //     ])
  //   } 
  //   drawCalendar(yearandMonth, calendarData);
  // });

}

// 功能：绘制日历图
// 参数：时间（如：2023-02），日历格式数据
// 返回：null
function drawCalendar(yearandMonth, calendarData){
  
  //初始化DOM以及配置项
  var calendarDom = document.getElementById('calendar');
  var calendarChart = echarts.init(calendarDom);
  var calendarOption;
  //位置信息
  const layouts = [
    [[0, 0]],
    [
      [-0.25, 0],
      [0.25, 0]
    ],
    [
      [0, -0.2],
      [-0.2, 0.2],
      [0.2, 0.2]
    ],
    [
      [-0.25, -0.25],
      [-0.25, 0.25],
      [0.25, -0.25],
      [0.25, 0.25]
    ]
  ];

  //图标矢路径
  const pathes = [
    'M512.871857 751.024885c-131.681088 0-238.812037-107.130949-238.812037-238.812037 0-131.683135 107.130949-238.814084 238.812037-238.814084 131.683135 0 238.814084 107.130949 238.814084 238.814084C751.684918 643.893936 644.554992 751.024885 512.871857 751.024885zM512.871857 313.414129c-109.61656 0-198.796672 89.180112-198.796672 198.798718 0 109.61656 89.180112 198.796672 198.796672 198.796672 109.617583 0 198.798718-89.180112 198.798718-198.796672C711.670575 402.594241 622.48944 313.414129 512.871857 313.414129zM512.642636 223.407186c-11.048637 0-20.007683-8.958022-20.007683-20.007683L492.634954 83.752496c0-11.04966 8.958022-20.007683 20.007683-20.007683 11.04966 0 20.007683 8.958022 20.007683 20.007683l0 119.645984C532.650319 214.449163 523.692297 223.407186 512.642636 223.407186zM512.642636 961.40743c-11.048637 0-20.007683-8.958022-20.007683-20.007683L492.634954 821.75274c0-11.048637 8.958022-20.007683 20.007683-20.007683 11.04966 0 20.007683 8.958022 20.007683 20.007683l0 119.647007C532.650319 952.449407 523.692297 961.40743 512.642636 961.40743zM288.160979 308.10317c-5.120624 0-10.240225-1.952468-14.148231-5.860475l-78.743412-78.744435c-7.813966-7.813966-7.812943-20.481474 0.001023-28.29544 7.812943-7.81192 20.48045-7.812943 28.29544 0.001023l78.743412 78.744435c7.813966 7.813966 7.813966 20.481474 0 28.29544C298.401204 306.149679 293.279556 308.10317 288.160979 308.10317zM815.865659 835.806827c-5.120624 0-10.240225-1.953492-14.148231-5.859451l-78.743412-78.743412c-7.813966-7.813966-7.813966-20.482497 0-28.29544 7.814989-7.81192 20.48045-7.81192 28.29544 0l78.743412 78.743412c7.813966 7.813966 7.813966 20.482497 0 28.296463C826.106908 833.853335 820.987307 835.806827 815.865659 835.806827zM203.464995 532.583804 83.819011 532.583804c-11.04966 0-20.007683-8.958022-20.007683-20.007683s8.958022-20.007683 20.007683-20.007683l119.645984 0c11.04966 0 20.007683 8.958022 20.007683 20.007683S214.515678 532.583804 203.464995 532.583804zM941.465239 532.583804 821.817208 532.583804c-11.048637 0-20.007683-8.958022-20.007683-20.007683s8.959046-20.007683 20.007683-20.007683l119.647007 0c11.048637 0 20.007683 8.958022 20.007683 20.007683S952.513876 532.583804 941.465239 532.583804zM209.417567 835.806827c-5.120624 0-10.241248-1.953492-14.148231-5.859451-7.812943-7.813966-7.812943-20.482497 0-28.296463l78.743412-78.743412c7.813966-7.81192 20.48045-7.81192 28.296463 0 7.813966 7.813966 7.813966 20.482497 0 28.29544l-78.743412 78.743412C219.658815 833.853335 214.538191 835.806827 209.417567 835.806827zM737.12327 308.10317c-5.120624 0-10.240225-1.952468-14.148231-5.859451-7.813966-7.813966-7.813966-20.481474 0-28.29544l78.743412-78.744435c7.816013-7.813966 20.48045-7.814989 28.29544-0.001023 7.813966 7.813966 7.813966 20.481474 0 28.29544l-78.743412 78.744435C747.363496 306.149679 742.243895 308.10317 737.12327 308.10317z',
    'M937.63091 516.463662c-11.048637 0-20.007683-8.958022-20.007683-20.007683 0-31.29168-12.079106-60.85295-34.010605-83.240843-21.887496-22.341844-51.152007-35.109636-82.407871-35.94977-10.998495-0.296759-19.694551-9.419534-19.464307-20.421098 0.013303-0.582261-0.010233-1.147126-0.025583-1.715061-0.021489-0.833995-0.040932-1.672082-0.040932-2.53166 0-113.682155-92.533486-206.16857-206.27397-206.16857-88.11792 0-166.530804 55.966663-195.119933 139.26481-2.358721 6.872524-8.257058 11.927657-15.412015 13.202697-7.145747 1.27504-14.434757-1.428535-19.025309-7.061836-13.489222-16.564257-33.468253-26.063609-54.812373-26.063609-38.950104 0-70.63985 31.69077-70.63985 70.644967 0 5.622044 0.678452 11.259438 2.012843 16.752546 1.338485 5.501294 0.281409 11.311627-2.905166 15.991206-3.188622 4.678556-8.207939 7.788383-13.817704 8.556886-58.751079 8.05342-103.053074 58.935274-103.053074 118.354572 0 11.048637-8.958022 20.007683-20.008706 20.007683s-20.007683-8.958022-20.007683-20.007683c0-72.519664 49.461505-135.51644 117.909433-154.002466-0.096191-1.883907-0.144286-3.768837-0.144286-5.653767 0-61.018725 49.63956-110.660332 110.655216-110.660332 22.745027 0 44.4954 6.884804 62.72867 19.454074C394.501936 161.119561 480.208946 106.413612 575.398936 106.413612c131.204228 0 238.775198 103.089913 245.912759 232.486982 34.43937 5.028527 66.185398 21.098527 90.885963 46.311769 29.302373 29.912263 45.440935 69.419046 45.440935 111.243617C957.638593 507.505639 948.680571 516.463662 937.63091 516.463662zM935.87901 600.288812 81.552387 600.288812c-11.04966 0-20.007683-9.417487-20.007683-20.466124s8.958022-20.466124 20.007683-20.466124L935.87901 559.356564c11.048637 0 20.007683 9.417487 20.007683 20.466124S946.927647 600.288812 935.87901 600.288812zM723.797777 724.108862 81.552387 724.108862c-11.04966 0-20.007683-8.905834-20.007683-19.954471s8.958022-19.954471 20.007683-19.954471l642.24539 0c11.048637 0 20.007683 8.905834 20.007683 19.954471S734.847437 724.108862 723.797777 724.108862zM493.710448 852.022137 81.552387 852.022137c-11.04966 0-20.007683-8.905834-20.007683-19.954471s8.958022-19.954471 20.007683-19.954471l412.157038 0c11.048637 0 20.007683 8.905834 20.007683 19.954471S504.759085 852.022137 493.710448 852.022137z',
    'M939.940512 225.758744 83.559091 225.758744c-11.050684 0-20.008706-8.903787-20.008706-19.954471s8.958022-19.954471 20.008706-19.954471l856.382445 0c11.04966 0 20.008706 8.903787 20.008706 19.954471S950.990173 225.758744 939.940512 225.758744zM939.940512 841.789075 83.559091 841.789075c-11.050684 0-20.008706-8.904811-20.008706-19.954471 0-11.04966 8.958022-19.954471 20.008706-19.954471l856.382445 0c11.04966 0 20.008706 8.904811 20.008706 19.954471C959.949218 832.885288 950.990173 841.789075 939.940512 841.789075zM287.649326 391.534348 153.590074 391.534348c-11.050684 0-20.008706-8.903787-20.008706-19.954471s8.958022-19.954471 20.008706-19.954471l134.060275 0c11.04966 0 20.008706 8.903787 20.008706 19.954471S298.700009 391.534348 287.649326 391.534348zM583.78186 391.534348 449.721585 391.534348c-11.04966 0-20.008706-8.903787-20.008706-19.954471s8.959046-19.954471 20.008706-19.954471l134.060275 0c11.04966 0 20.008706 8.903787 20.008706 19.954471S594.83152 391.534348 583.78186 391.534348zM881.914958 391.534348 747.854683 391.534348c-11.04966 0-20.008706-8.903787-20.008706-19.954471s8.959046-19.954471 20.008706-19.954471l134.060275 0c11.04966 0 20.008706 8.903787 20.008706 19.954471S892.964618 391.534348 881.914958 391.534348zM287.649326 692.38637 153.590074 692.38637c-11.050684 0-20.008706-9.416464-20.008706-20.466124 0-11.04966 8.958022-20.466124 20.008706-20.466124l134.060275 0c11.04966 0 20.008706 9.416464 20.008706 20.466124C307.659055 682.97093 298.700009 692.38637 287.649326 692.38637zM583.78186 692.38637 449.721585 692.38637c-11.04966 0-20.008706-9.416464-20.008706-20.466124 0-11.04966 8.959046-20.466124 20.008706-20.466124l134.060275 0c11.04966 0 20.008706 9.416464 20.008706 20.466124C603.790566 682.97093 594.83152 692.38637 583.78186 692.38637zM881.914958 692.38637 747.854683 692.38637c-11.04966 0-20.008706-9.416464-20.008706-20.466124 0-11.04966 8.959046-20.466124 20.008706-20.466124l134.060275 0c11.04966 0 20.008706 9.416464 20.008706 20.466124C901.923664 682.97093 892.964618 692.38637 881.914958 692.38637zM421.709601 535.820522 285.648762 535.820522c-11.04966 0-20.008706-8.904811-20.008706-19.954471s8.959046-19.954471 20.008706-19.954471l136.060839 0c11.04966 0 20.008706 8.904811 20.008706 19.954471S432.759261 535.820522 421.709601 535.820522zM751.856833 535.820522 617.796558 535.820522c-11.04966 0-20.008706-8.904811-20.008706-19.954471s8.959046-19.954471 20.008706-19.954471l134.060275 0c11.04966 0 20.008706 8.904811 20.008706 19.954471S762.906493 535.820522 751.856833 535.820522z',
    'M776.512 64C914.645333 69.098667 1024 182.570667 1024 320.789333c0 138.24-109.354667 251.712-247.488 256.810667H34.965333a34.965333 34.965333 0 0 1 0-69.930667h741.546667a187.050667 187.050667 0 0 0 0-373.76 187.050667 187.050667 0 0 0-187.050667 186.709334 34.965333 34.965333 0 0 1-69.930666 0A256.64 256.64 0 0 1 776.533333 64z m-130.069333 604.16h1.749333a146.133333 146.133333 0 0 1 0 291.925333 142.997333 142.997333 0 0 1-88.789333-30.421333 144.384 144.384 0 0 1-58.752-115.733333 34.965333 34.965333 0 1 1 69.930666 0 75.178667 75.178667 0 0 0 29.717334 60.138666 74.112 74.112 0 0 0 46.144 16.085334 76.224 76.224 0 0 0 0-152.106667H195.456a34.965333 34.965333 0 0 1 0-69.909333h450.986667zM34.986667 417.088a34.965333 34.965333 0 0 1 0-69.909333h320.938666a34.965333 34.965333 0 1 1 0 69.909333H34.986667z',
    'M800.276612 662.542668 225.189785 662.542668c-87.853907 0-159.326728-71.408353-159.326728-159.184489 0-72.360028 49.345871-135.220704 117.636211-153.686265-0.096191-1.866511-0.14224-3.736091-0.14224-5.603625 0-60.904115 49.547463-110.453624 110.449531-110.453624 22.681582 0 44.373627 6.859221 62.564941 19.386536 40.666188-83.883479 126.177747-138.452305 221.149772-138.452305 130.917702 0 238.254336 102.845343 245.390873 231.957933 34.355459 5.02341 66.021669 21.060665 90.663906 46.213531 29.245068 29.850865 45.349861 69.278853 45.349861 111.018489 0 42.358737-16.700357 82.276888-47.02706 112.404046C881.781928 646.065392 842.139046 662.542668 800.276612 662.542668zM293.806559 273.633101c-38.835494 0-70.432119 31.597649-70.432119 70.436212 0 5.603625 0.676405 11.223622 2.006703 16.702404 1.338485 5.502317 0.281409 11.313673-2.905166 15.991206-3.188622 4.679579-8.208962 7.788383-13.818727 8.558933-58.592466 8.032954-102.776781 58.777685-102.776781 118.037347 0 65.708538 53.521984 119.166053 119.309316 119.166053l575.086827 0c31.223119 0 60.84988-12.349259 83.417875-34.770921 22.707165-22.556739 35.21299-52.394301 35.21299-84.014462 0-31.205723-12.046361-60.687174-33.917484-83.013669-21.828145-22.281469-51.016931-35.015491-82.186837-35.855626-11.000542-0.294712-19.696598-9.420557-19.465331-20.423145 0.011256-0.578168-0.010233-1.13894-0.025583-1.705851-0.021489-0.831948-0.040932-1.671059-0.040932-2.52859 0-113.393583-92.299149-205.64566-205.750037-205.64566-87.896886 0-166.111249 55.824423-194.627722 138.912793-2.358721 6.874571-8.258081 11.927657-15.413038 13.202697-7.147794 1.27197-14.435781-1.426489-19.026332-7.061836C335.00282 283.105846 315.086212 273.633101 293.806559 273.633101zM358.972744 819.561841c-11.04966 0-20.008706-8.959046-20.008706-20.008706l0-68.988234c0-11.04966 8.959046-20.008706 20.008706-20.008706s20.008706 8.959046 20.008706 20.008706l0 68.988234C378.981451 810.602795 370.022405 819.561841 358.972744 819.561841zM501.68712 939.616124c-11.04966 0-20.008706-8.959046-20.008706-20.008706L481.678414 810.601772c0-11.04966 8.959046-20.008706 20.008706-20.008706 11.04966 0 20.008706 8.959046 20.008706 20.008706L521.695826 919.607418C521.695826 930.657079 512.73678 939.616124 501.68712 939.616124zM644.402519 819.561841c-11.04966 0-20.008706-8.959046-20.008706-20.008706l0-68.988234c0-11.04966 8.959046-20.008706 20.008706-20.008706 11.04966 0 20.008706 8.959046 20.008706 20.008706l0 68.988234C664.411225 810.602795 655.452179 819.561841 644.402519 819.561841z',
    'M913.902486 396.369469c-24.63405-25.14468-56.262398-41.21775-90.583065-46.339398C815.932174 220.757846 708.377576 117.856221 577.230654 117.856221c-95.02626 0-180.608427 54.472636-221.50793 138.26504-18.184151-12.360516-39.795355-19.12457-62.380746-19.12457-61.253062 0-111.087051 49.835012-111.087051 111.090121 0 1.727341 0.039909 3.454682 0.12075 5.179976-68.349691 18.690688-117.686353 81.69872-117.686353 154.208151 0 88.143503 71.773673 159.852708 159.995971 159.852708l104.141872 0 0 269.705652c0 11.370978 9.219989 20.590967 20.590967 20.590967 11.370978 0 20.590967-9.219989 20.590967-20.590967L370.009102 667.327648l188.599426 0 0 269.705652c0 11.370978 9.219989 20.590967 20.590967 20.590967 11.370978 0 20.590967-9.219989 20.590967-20.590967L599.790462 667.327648 800.12721 667.327648c42.039465 0 81.85017-16.546861 112.091938-46.592155 30.453592-30.255071 47.223534-70.342068 47.223534-112.87886C959.442682 465.942011 943.270351 426.347225 913.902486 396.369469zM883.195114 591.521125c-22.476921 22.327518-51.976792 34.625612-83.067904 34.625612L224.684272 626.146737c-65.514109 0-118.814036-53.236482-118.814036-118.671797 0-59.013045 44.00012-109.546975 102.348016-117.545136 5.773494-0.789992 10.94119-3.991917 14.220886-8.809643 3.281743-4.815679 4.367471-10.79588 2.990101-16.458857-1.323135-5.430686-1.992377-11.008728-1.992377-16.575514 0-38.546921 31.358195-69.90921 69.905116-69.90921 21.118993 0 40.889269 9.402137 54.241368 25.794479 4.723581 5.79703 12.213159 8.582469 19.580964 7.267521 7.361665-1.312902 13.431917-6.513344 15.859199-13.58746 28.455075-82.909292 106.500593-138.613988 194.207144-138.613988 113.208365 0 205.311039 92.053556 205.311039 205.203592 0 0.856507 0.020466 1.693572 0.039909 2.523473 0.016373 0.560772 0.037862 1.116427 0.024559 1.689479-0.245593 11.326976 8.707312 20.72502 20.031219 21.028942 31.043017 0.836041 60.109006 13.516852 81.846076 35.706223 21.781072 22.233374 33.777291 51.593052 33.777291 82.667791C918.261771 539.344788 905.809157 569.05546 883.195114 591.521125zM464.307791 701.314717c-11.370978 0-20.590967 9.219989-20.590967 20.590967l0 215.127615c0 11.370978 9.219989 20.590967 20.590967 20.590967 11.370978 0 20.590967-9.219989 20.590967-20.590967L484.898759 721.905684C484.898759 710.533682 475.679793 701.314717 464.307791 701.314717zM694.087105 701.314717c-11.370978 0-20.590967 9.219989-20.590967 20.590967l0 215.127615c0 11.370978 9.219989 20.590967 20.590967 20.590967s20.590967-9.219989 20.590967-20.590967L714.678072 721.905684C714.678072 710.533682 705.458083 701.314717 694.087105 701.314717zM234.527454 763.086595c-11.370978 0-20.590967 9.219989-20.590967 20.590967l0 91.583858c0 11.370978 9.219989 20.590967 20.590967 20.590967 11.370978 0 20.590967-9.219989 20.590967-20.590967l0-91.583858C255.118422 772.305561 245.898433 763.086595 234.527454 763.086595zM808.978808 763.086595c-11.370978 0-20.590967 9.219989-20.590967 20.590967l0 91.583858c0 11.370978 9.219989 20.590967 20.590967 20.590967 11.370978 0 20.590967-9.219989 20.590967-20.590967l0-91.583858C829.568752 772.305561 820.349787 763.086595 808.978808 763.086595z'
  ];

  //图标颜色
  const colors = [
    // '#808080',
    // '#72AEFD',
    // '#8FBC8F',
    // '#FDC763',
    // '#BDB76B',
    // '#c4332b',
    '#f1c40f'
  ];

  //车流量视觉映射
  const trafficColors = [
    '#313695',
    '#4575b4',
    '#74add1',
    '#abd9e9',
    '#e0f3f8',
    '#ffffbf',
    '#fee090',
    '#fdae61',
    '#f46d43',
    '#d73027',
    '#a50026'
  ];

  //配置项
  calendarOption = {
    title: {
      text: '日常车流量与天气现象',
      textStyle:{
        color:'	#FFFFFF',
      }
    },
    tooltip: {
      formatter: function (params) {
        return params.data[0] + "</br>"
          + "天气：" + params.data[2] + "</br>"
          // +"气温："+Math.ceil(params.data[6])+"℃"+"</br>"
          + "质量等级：" + params.data[5] + "</br>"
          + "总车流量：" + params.data[3] + "</br>"
          + "";

      }
    },
    calendar: [
      {
        left: 'center',
        bottom:20,
        cellSize: [60, 45], //日历格子大小[长，宽]
        yearLabel: { show: false },
        orient: 'vertical',
        // dayLabel: {
        //   firstDay: 1,
        //   nameMap: 'cn',
        //   color:'	#FFFFFF',
        // },
        monthLabel: {
          fontSize: '1.5em',
          color:'	#FFFFFF',
        },
        range: yearandMonth
      }
    ],
    series: [
      //自定义图标和颜色映射
      {
        type: 'custom',
        coordinateSystem: 'calendar',
        renderItem: function (params, api) {
          const cellPoint = api.coord(api.value(0));
          const cellWidth = params.coordSys.cellWidth;
          const cellHeight = params.coordSys.cellHeight;
          const value = api.value(1);
          const events = value && value.split('|');
          if (isNaN(cellPoint[0]) || isNaN(cellPoint[1])) {
            return;
          }
          //渲染图标
          const group = {
            type: 'group',
            children:
              (layouts[events.length - 1] || []).map(function (
                itemLayout,
                index
              ) {
                return {
                  type: 'path',
                  shape: {
                    pathData: pathes[+events[index]],
                    x: -8,
                    y: -8,
                    width: 10,
                    height: 10
                  },
                  position: [
                    cellPoint[0] +
                    echarts.number.linearMap(
                      itemLayout[0],
                      [-0.5, 0.5],
                      [-cellWidth / 2, cellWidth / 2]
                    ),
                    cellPoint[1] +
                    echarts.number.linearMap(
                      itemLayout[1],
                      [-0.5, 0.5],
                      [-cellHeight / 2 + 20, cellHeight / 2]
                    )
                  ],
                  style: api.style({
                    fill: colors[+events[index]] - 10
                  })
                };
              }) || []
          };

          //渲染文字
          group.children.push({
            type: 'text',
            style: {
              x: cellPoint[0]+4,
              y: cellPoint[1] - cellHeight / 2 + 10,
              text: echarts.format.formatTime('dd', api.value(0)),
              textFont: api.font({ fontSize: 12 }),
            }
          });

          //渲染颜色
          group.children.push({
            type: 'rect',
            shape: {
              x: cellPoint[0] - cellWidth / 2,
              y: cellPoint[1] - cellHeight / 2,
              width: cellWidth,
              height: cellHeight
            },
            style: {
              fill: trafficColors[api.value(4)],
            }
          });

          //转置渲染顺序
          group.children.reverse();
          return group;
        },
        dimensions: [undefined, { type: 'ordinal' }],
        data: calendarData
      }
    ]
  };

  calendarOption && calendarChart.setOption(calendarOption);

  //使用.off('')或.unbind('')解决一次触发多次执行
  calendarChart.off('click').on("click", params => {
    let systemDate = params.data[0];
    console.log('调用................')
    callFigures(systemDate);
    document.getElementById("myDate").value = systemDate;

	})

}
